package com.cjy.dao;


import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    private static Logger logger = Logger.getLogger(BaseDao.class);
    //驱动
    private static String DRIVER = null;
    //数据库连接地址
    private static String URL = null;
    //用户名
    private static String USER = null;
    //密码
    private static String PWD = null;
    //数据库连接对象
    Connection conn = null;
    //指令操作对象
    PreparedStatement pstmt = null;
    //结果集
    ResultSet rs = null;

    static {
        String configFile = "database.properties";
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(configFile);
        Properties properties = new Properties();
        try {
            //读取配置文件信息
            properties.load(is);
            DRIVER = properties.getProperty("driver");
            URL = properties.getProperty("url");
            USER = properties.getProperty("user");
            PWD = properties.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //加载驱动
        try {
            Class.forName(DRIVER);
            logger.debug("驱动加载成功！");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //打开数据库连接
    public Connection openConn() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL, USER, PWD);
            logger.debug("数据库连接成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    //增删改的方法
    public int executeUpdate(String sql, Object... params) {
        //打开连接
        conn = openConn();
        try {
            logger.debug("增删改操作指令：" + sql);
            pstmt = conn.prepareStatement(sql);
            if (null != params) {
                for (int i = 0; i < params.length; i++) {
                    pstmt.setObject(i + 1, params[i]);
                }
            }
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closedAll(null);
        }
        return -1;
    }

    //获取查询数据集
    public ResultSet executeQuery(String sql, Object... params) {
        //打开连接
        conn = openConn();
        try {
            logger.debug("执行查询指令："+sql);
            pstmt = conn.prepareStatement(sql);
            if (null != params) {
                for (int i = 0; i < params.length; i++) {
                    pstmt.setObject(i + 1, params[i]);
                }
            }
            return pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //关闭连接
    public void closedAll(ResultSet rs){
        try {
            if(rs!=null){
                rs.close();
            }
            if(pstmt!=null){
                pstmt.close();
            }
            if (conn!=null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        logger.debug("连接关闭成功");
    }
}
